package com.hqc.util;

import java.io.IOException;
import java.io.Writer;
import java.util.Map;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import freemarker.template.TemplateModelException;
import freemarker.template.TemplateScalarModel;

/**
 * Freemaker格式化时间戳为指定日期
 * 
 * @author Joey
 * @project:hqc_mp
 * @date：2017年5月27日
 * 
 */
public class FormatTimeFTLHelper implements TemplateDirectiveModel {

	@Override
	public void execute(Environment env,
			@SuppressWarnings("rawtypes") Map params, TemplateModel[] loopVars,
			TemplateDirectiveBody body) throws TemplateException, IOException {
		if (body == null) {// 自定义标签必须有内容，即自定义 开始标签与结束标签之间必须有 内容
			throw new TemplateModelException(
					"自定义标签必须有内容，即自定义开始标签与结束标签之间必须有内容");
		}
		Writer out = env.getOut();
		TemplateScalarModel scalarModel = (TemplateScalarModel) params
				.get("unix");
		if (null == scalarModel) {
			return;
		}
		TemplateScalarModel pattern = (TemplateScalarModel) params
				.get("pattern");
		String asString;
		if (pattern == null) {
			asString = DateUtils.DATE_TIME_PATTERN;
		} else {
			asString = pattern.getAsString();
		}
		long flag = Long.valueOf(scalarModel.getAsString());
		out.write(DateUtils.format(JoeyUtil.fomartDate(flag * 1000), asString));
		body.render(out);
	}

}
